import openai, tiktoken


# openai.api_base = "https://api.wzunjh.top/v1"
# openai.api_key = "sk-Fkphr3IyUwcJQcVx68CeA5B82fA14aEdA034A91323250d01"
#
# receive = ''
# send = "你好呀"

# completion = openai.ChatCompletion.create(
#     model="gpt-3.5-turbo",
#     messages=[
#         {"role": "user", "content": send}
#     ],
#     stream=True,
# )
# for line in completion:
#     if line["choices"][0]["finish_reason"] == 'stop':
#         break
#     receive += line["choices"][0]["delta"]["content"]
#
# print(receive)

# receive = completion.choices[0]["message"]["content"]

# send_token = len(encoding.encode(send)) + 7
# receive_token = len(encoding.encode(receive))
#
# print(f"发送内容：{send}")
# print(f"实际发送使用了{completion['usage']['prompt_tokens']}个token")
# print(f"tiktoken计算发送使用了{send_token}个token")
#
# print(f"接收内容：{receive}")
# print(f"实际计算接收使用了{completion['usage']['completion_tokens']}个token")
# print(f"tiktoken计算接收使用了{receive_token}个token")

# import requests
#
# response = requests.post(
#     url='https://api.wzunjh.top/v1/chat/completions',
#     headers={
#         "Content-Type": "application/json",
#         "Authorization": "Bearer sk-Fkphr3IyUwcJQcVx68CeA5B82fA14aEdA034A91323250d01"
#     },
#     data={
#         "model": 'chatcmpl-123',
#         "messages": [{"role": "user", "content": "你好"}],
#         "temperature": 0.7,
#         "stream": True,
#     },
#     stream=True
# )
#
# string = b''
# for chunk in response.iter_content():
#     string += chunk
# print(string.decode())


# import openai
#
# completion = openai.ChatCompletion.create(
#     api_key='sk-Fkphr3IyUwcJQcVx68CeA5B82fA14aEdA034A91323250d01',
#     api_base='https://api.wzunjh.top/v1',
#     model="gpt-3.5-turbo",
#     stream=True,
#     messages=[
#         {"role": "system", "content": "You are a helpful assistant."},
#         {"role": "user", "content": "Hello!"}
#     ]
# )
#
# reply = ''
# for line in completion:
#     print(line)
#     if line["choices"][0]["finish_reason"] is not None:
#         data = ''
#     else:
#         data = line["choices"][0]["delta"].get("content", "")
#         reply += data
#
# print(reply)

# import tiktoken
# encoding = tiktoken.encoding_for_model('gpt-4')
# message = [{"content": "Hello", "role": "user"}, {"content": "World", "role": "system"}]
# count = sum(len(encoding.encode(item['content'])) + 7 for item in message)
# print(count)



